// xc_lshop/savemoney/recharge/com/phone.js
var order_bill = require('../../../model/htrecharge')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    config:Object
  },
  

  /**
   * 组件的初始数据
   */
  data: {
    keyval:"",
    typename:{
       1:"移动",
       2:"电信",3:"联通",4:"虚拟"
    },
    phone:"",
    type:"-1",
    numdata:{
      1:[134,135,136,137,138,139,144,147,148,150,151,152,157,158,159,165,1703,1705,1706,172,178,182,183,184,187,188,195,197,198,1440,1703,1705,1706],
      2:[133,141,149,153,162,1700,1701,1702,173,1740,177,180,181,189,190,191,193,199,1349],
      3:[130,131,132,140,145,146,155,156,166,167,175,176,185,186,196,1704,1707,1708,1709,1710,1711,1712,1713,1714,1715,1716,1717,1718,1719]
    }
  },
  ready(){
    console.log(this.data.config)
    this.getitems()
    // var that=this
    // setTimeout( function(){
    //   var newnav= that.data.nav
    //   if(that.data.config && that.data.config.type){
    //     var type=that.data.config.type
    //      for(var i in type){
    //        newnav[type[i]-1].disabled=-1
    //      }
    //   }
    //   that.setData({
    //     nav:newnav
    //   })
      
    //   }, 500 );
   

 },
  /**
   * 组件的方法列表
   */
  methods: {
    //完成手机号输入
    onChange(e){
      
       var phone=e.detail;
       if(phone){
         this.isphone(phone);
       }
      //  this.chuanzhi("number",e.detail)
    },
    getitems(){
      var that=this;
      order_bill.getitems().then(function(res){
        if(res['status']==1){
          that.setData({
            modellist:res['obj']
          })
        }
      })
    },
    isphone(phone){
    
        if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
          this.setData(
            {type:-1}
          )
           return false
        }else{
          var numdata=this.data.numdata
          var update={}
          update['phone']=phone
          Object.keys(numdata).forEach(function(key){
          
            for(var i in numdata[key]){
                if(phone.indexOf(numdata[key][i])==0){
                  update['type']=key;
                  break;
                }
            }
          })
          
     

          // if( /^1(3[4-9]|5[012789]|8[23478]|4[778]|7[28]|9[578])\d{8}$/.test(phone)){
          //   update['type']="1"
          // }else if(/^1(3[0-2]|5[56]|8[56]|4[5]|7[6])\d{8}$/.test(phone)){
          //   update['type']="3"
          // }else if(/^1(3[3])|(8[019])|(5[3])|(7[37])|(9[0139])\d{8]$/.test(phone)){
          //   update['type']="2"
          // }else{
          //   update['type']="4"
          // }
          console.log("update")
          console.log(update)
          this.setData(
            update
          )
          return true
        }
      
    },
    onChoose(e){

      if(this.data.type && this.data.phone){
        var item=e.currentTarget.dataset.item;
        console.log(item)
        var addprice=this.data.config.addprice
        if(item.price && addprice){
          var realprice=item.price * 1 + item.price * addprice * 0.01;
          var pows=Math.pow(10,2)
          item.realprice = Math.round(realprice  * pows) / pows;
        }
        item.mobile=this.data.phone
        item.itype=this.data.type
        this.triggerEvent('createorder',item);
      }else{
        wx.showToast({
          icon:"none",
          title: '请先输入充值号码',
        })
      }
      //
      // console.log(dataset.item)
      // this.setData({
      //   itemindex:dataset.index
      // })
      // this.chuanzhi("item",dataset.item)
    },
    onClickNav(e){
      var dataset=e.currentTarget.dataset;
      if(dataset.disabled==-1){
        this.setData({
          keyval:dataset.val
        })
        this.chuanzhi("type",e.currentTarget.dataset.val)
      }
     
    },
    inputtext(e){
     

      this.chuanzhi("number",e.detail)
    },
    // onChange(e){
    //   console.log("onChange")
    //   console.log(e)
    //   var val=!this.data.checked
    //   this.setData({
    //     checked:val
    //   })
    //   this.chuanzhi("ischecked",val)
      
    // },
    
    chuanzhi(name,data){
      var data={
        name:name,
        data:data
      }
      this.triggerEvent('chuanzhi', data, {})
    }
  }
})
